
var UserData = require('UserData')
var Breath = require('Breath')
var Float = require('Float')
var CUSTOMER_DATA = require('CustomerData')
var customers = require('customers')
var resLoader = require('resLoader')
//var COMMON_VALUE = require('Common')

cc.Class({
    extends: cc.Component,
    properties: {
        coinPrefab:{
            default:null,
            type:cc.Prefab
        }
    },
    //moveAction
    go () {
        this.desinationRes        
        this.direction = 1
        this.node.x = this.desinationRes.x + Math.random()*(this.desinationRes.width -this.node.width) - (this.desinationRes.width -this.node.width)/2
        this.node.y = this.desinationRes.y
        this.posY = 10
        this.node.y = this.posY
        this.floorHeight = this.desinationRes.height
        this.node.getChildByName('CustomerSprite').getComponent('ActionIn').playAnime(this.move,this)
    },
    move(self){
        if(self.getComponent('Customer')){
            self = self.getComponent('Customer')
        }
        //self.node.y = 20
        if(Math.random()<0.5){//50%概率移动
            self.speed = Math.random() * 200+50
            let desX = self.desinationRes.x + Math.random()*(self.desinationRes.width -self.node.width) - (self.desinationRes.width -self.node.width)/2
            let distant = Math.abs(self.node.x - desX) 
            let distantVec = self.node.x - desX
            let duration = distant/self.speed
            duration = duration<1?1:duration
            
            let action = cc.jumpTo(duration,cc.v2(desX,self.posY),20,Math.floor(duration*3))
            if(self.customerData.jumpHeight){//跳跃高度有要求的单位：比如史莱姆
                action = cc.jumpTo(duration,cc.v2(desX,self.posY),self.customerData.jumpHeight,Math.floor(duration))
            }

            if(self.customerData.floatingHeight){//使用漂浮的单位：红龙和精灵
                this.node.addComponent("Float")
                self.posY = self.customerData.floatingHeight
                //self.node.y = self.posY
                action = cc.moveTo(duration,cc.v2(desX,self.posY))
            }
            
            self.moveCycle = cc.sequence(action,cc.callFunc(self.move,self.getComponent('Customer')))

            if(self.node.x - desX>0 && self.direction<0 || self.node.x - desX<0 && self.direction>0){
                self.node.runAction(self.moveCycle)
            }else{//转向
                self.direction = self.direction ===1?-1:1
                self.node.getChildByName('CustomerSprite').getComponent('Turn').turn()
                self.node.runAction(self.moveCycle)
                if(self.speed>150){
                    self.node.getChildByName('smoke').x = self.direction * (-150)
                    self.node.getChildByName('smoke').scaleX = -self.direction
                    self.node.getChildByName('smoke').getComponent(cc.Animation).play()
                }
            }
        }else{//50%概率停止
            self.node.getChildByName('CustomerSprite').getComponent('Breath').takeBreath()
            let action = cc.delayTime(Math.random()*5+2)
            self.moveCycle = cc.sequence(action,cc.callFunc(self.move,self.getComponent('Customer')))
            self.node.runAction(self.moveCycle)
            self.speed = 0
            if(Math.random()<0.3){//30%几率弹出
                self.popChatBubles()
            }
        }
        
    },
    disappear(time){
        if(!time){
            time = 1
        }
        let disappearAction = cc.fadeOut(1.0)
        this.node.runAction(cc.sequence(disappearAction,cc.callFunc(this._delete,this)))
    },
    _delete(){
        this.node.destroy()
    },

    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {
        this.coinList = []
        this.go()
        this.speed = Math.random() * 200+50
        this.AnimeSprite = this.node.getChildByName('CustomerSprite')
        if(!this.existedTime){
            this.existedTime = 0
        }
    },
    popChatBubles(){
        let chatNode = this.node.getChildByName('台词')
        let bubble = this.node.getChildByName('台词').getChildByName('对话框')
        bubble.getChildByName('name').getComponent(cc.Label).string = this.customerData.name

        if(this.node.x<-this.node.parent.width/2+300){
            chatNode.scaleX = -1
            bubble.getChildByName('lines').scaleX = -1
            bubble.getChildByName('name').scaleX = -1

        }else if(this.node.x>this.node.parent.width/2-300){
            chatNode.scaleX = 1
            bubble.getChildByName('lines').scaleX = 1
            bubble.getChildByName('name').scaleX = 1
        }
        
        bubble.getChildByName('lines').getComponent(cc.Label).string  = this.customerData.lines[Math.floor(Math.random()*this.customerData.lines.length)]
        let desScaleX = this.node.getChildByName('台词').scaleX
        chatNode.scaleX = 0
        chatNode.scaleY = 0
        chatNode.active = true
        chatNode.runAction(cc.sequence(
            cc.scaleTo(0.5,desScaleX,1).easing(cc.easeExponentialOut()),
            cc.delayTime(7),
            cc.scaleTo(0.5,0,0).easing(cc.easeExponentialIn()),
        ))
        
    },

    update (dt) {
        if(this.CustomerID){
            this.node.getChildByName("satisfiction").y = this.node.getChildByName('CustomerSprite').y+this.node.getChildByName('CustomerSprite').height +20
            this.node.getChildByName('台词').y = this.node.getChildByName("satisfiction").y  -30
            let CustomerData = CUSTOMER_DATA.LIST[parseInt(this.CustomerID)-1]
            if(CustomerData){
                this.existedTime+=dt
                if(this.existedTime<CustomerData.duration){
                    this.existedTime += dt 
                    let per = this.existedTime/CustomerData.duration
                    let sp = this.node.getChildByName('satisfiction').getComponent(cc.Sprite)
                    if(per<0.05){
                        resLoader.localImgLoader('Customer/红',sp)
                    }else if(per<0.35){
                        resLoader.localImgLoader('Customer/黄',sp)
                    }else{
                        resLoader.localImgLoader('Customer/绿',sp)
                    }
                    
                    //this.node.getChildByName('satisfiction').getChildByName('bar').color = new cc.Color(245 - per * 233, per * 233,70-per*40)
                }else{
                    this.existedTime = 0
                    //销毁
                    //删除远程
                    //如果是本地创建的，首先需要获取他在远程的ID才能删除，如果是远程数据创建的，可以直接使用ID删除//TOBEFINISHED
                    //customers
                    //删除本地node
                    let cl = this.node.parent.parent.parent.getComponent('Restaurant').customerList
                    for(let i=0;i<cl.length;i++){
                        if(cl[i].uuid ===this.node.uuid){
                            cl.splice(i,1)
                        }
                    }
                    this.node.destroy()
                }
            }
        }
    },
    
});
